package com.eascs.admin.o2o.repayment.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.dubbo.config.annotation.Reference;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.admin.o2o.repayment.service.O2oRepaymentService;
import com.eascs.admin.o2o.repayment.vo.O2oRepaymentVo;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;

@Controller
@RequestMapping("/repayment")
public class O2oRepaymentContorller extends BaseMasterViewController {
	@Override
	public void preHandle() {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "RepayMentData");
	}
	@Autowired
	O2oRepaymentService o2oRepaymentService;
	@Reference
	SysOrgService sysOrgService;
	
	@RequestMapping("/index")
	public String index(O2oRepaymentVo vo) {
		vo.setPageSize(100);
		List<ActOrgEntity> orgProvince = sysOrgService.getOrgProvince();
		ViewContext.put("ActOrgEntitys", orgProvince);
		String pageUrl = "/repayment/index";
		String billCode = vo.getBillCode() == null ? "" : vo.getBillCode();// 贷款单号
		String ltd = vo.getLtd() == null ? "" : vo.getLtd();// 省区
		String userName = vo.getUserName() == null ? "" : vo.getUserName();// 借款人姓名
		String jhStartTime = vo.getJhStartTime() == null ? "" : vo.getJhStartTime();// 计划还款日期【start】
		String jhEndTime = vo.getJhEndTime() == null ? "" : vo.getJhEndTime();// 计划还款日期【end】
		String fkStartTime = vo.getFkStartTime() == null ? "" : vo.getFkStartTime();// 放款日期【start】
		String fkEndTime = vo.getFkEndTime() == null ? "" : vo.getFkEndTime();// 放款日期【end】
		String dqStartTime = vo.getDqStartTime() == null ? "" : vo.getDqStartTime();// 到期日【start】
		String dqEndTime = vo.getDqEndTime() == null ? "" : vo.getDqEndTime();// 到期日【end】
		String bankName = vo.getBankName() == null ? "" : vo.getBankName();// 放款银行
		String operatorName = vo.getOperatorName() == null ? "" : vo.getOperatorName() ;// 信贷经理名字
		pageUrl = UrlUtil.appendUrl(pageUrl, "billCode", billCode);
		pageUrl = UrlUtil.appendUrl(pageUrl, "ltd", ltd);
		pageUrl = UrlUtil.appendUrl(pageUrl, "userName", userName);
		pageUrl = UrlUtil.appendUrl(pageUrl, "jhStartTime", jhStartTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "jhEndTime", jhEndTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "fkStartTime", fkStartTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "fkEndTime", fkEndTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "dqStartTime", dqStartTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "dqEndTime", dqEndTime);
		pageUrl = UrlUtil.appendUrl(pageUrl, "bankName", bankName);
		pageUrl = UrlUtil.appendUrl(pageUrl, "operatorName", operatorName);
		pageUrl = pageUrl + "&pageIndex=";
		PageResult<Object[]> pageResult = o2oRepaymentService.findDataWithPage(vo);
		String[] titles = { "贷款单号", "省区", "借款人姓名", "信贷经理","还款期数", "计划还款日期", "剩余本金", "计划还款本金", "计划还款利息", "还款方式", "放款日期", "到期日", "放款银行", "放款金额(万元)" };
		//Button[] titleButtons = new Button[] { ButtonHelper.createOpenButton(null, "/detail?loanapplyNo=") };
		Format[] formats = new Format[] { null, null,null, null, null, null, new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), new DecimalFormat(",##0.00"), null, null, null, null, new DecimalFormat(",##0.00") };
		SelectDataOption selectDataOptions[] = new SelectDataOption[]{null, null, null, null, null, null, null, null, null, new SelectDataOption("OTOREPAYMENTWAY"), null, null,new SelectDataOption("BANKTYPEO2O"),  null };
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, pageUrl);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		//o2oPagingInfo.setTitleButtons(titleButtons);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFormats(formats);
		ViewContext.put("paginationInfo", o2oPagingInfo);
		ViewContext.put("vo", vo);
		return "repayment/index";
	}
}
